// ==UserScript==
// @namespace   ame-applemusic
// @name        Ame (Apple Music)
// @version     1.4.3
// @author      SuperSaltyGamer
// @run-at      document-start
// @match       https://music.apple.com/*
// @grant       GM.addStyle
// @grant       GM.setClipboard
// @grant       GM.xmlHttpRequest
// ==/UserScript==

import { observe, waitFor } from '../common';
import { offAlbumRoute, onAlbumRoute } from './glue/routing';
import { hideButtonElement, showButtonElement } from './glue/ui/sidebar';
import './modules/badges';
import { checkCountriesButtonEl } from './modules/countries';
import { searchCoversButtonEl } from './modules/covers';
import { copyAuthButtonEl } from './modules/dev';
import { checkQualitiesButtonEl } from './modules/qualities';
import './modules/qualities';
import './modules/lyrics';
import styles from './style.css?inline';

GM.addStyle(styles);

// Add sidebar button for all pages.
waitFor('nav', 'amp-chrome-player').then((navEl) => {
	if (!navEl) return;

	showButtonElement(copyAuthButtonEl, 0);
});

// Add sidebar buttons for album page.
onAlbumRoute(async () => {
	showButtonElement(checkCountriesButtonEl, 100);
	showButtonElement(checkQualitiesButtonEl, 200);
	showButtonElement(searchCoversButtonEl, 300);
});

// Remove sidebar buttons for non-album pages.
offAlbumRoute(() => {
	hideButtonElement(searchCoversButtonEl);
	hideButtonElement(checkCountriesButtonEl);
});

// Hide trial upselling modal.
observe('iframe[src^="/includes/commerce/subscribe"]', () => {
	const backdropEl = document.querySelector<HTMLElement>('.backdrop');
	backdropEl?.click();
});
